import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:inmotion_pro/config/r.dart';
import 'package:inmotion_pro/l10n/local.dart';
import 'package:inmotion_pro/routes/app_routes.dart';
import 'package:lx_widgets_library/lx_widgets_library.dart';

import '../../login_register/area_code/country_model.dart';

// 设置用户所属地区
class SetUserRegionView extends StatefulWidget {
  // resultCallback
  final Function(AreaCodeModel, Function(bool)) onSelected;

  const SetUserRegionView({super.key, required this.onSelected});

  @override
  SetUserRegionViewState createState() => SetUserRegionViewState();
}

class SetUserRegionViewState extends State<SetUserRegionView> {
  // 当前选择的国家
  Rxn<AreaCodeModel> areaCodeModel = Rxn<AreaCodeModel>();

  @override
  Widget build(BuildContext context) {
    return ClipRRect(
      borderRadius: const BorderRadius.only(
        topLeft: Radius.circular(8.0),
        topRight: Radius.circular(8.0),
      ),
      child: Container(
        padding: EdgeInsets.only(
          left: CONTENT_MARGIN,
          right: CONTENT_MARGIN,
          top: CONTENT_MARGIN * 2,
          bottom: CONTENT_MARGIN + tabBarSpace,
        ),
        height: 320.w,
        child: Stack(
          children: [
            Column(
              children: [
                LxText(
                  local.select_country,
                  color: Colors.white,
                  fontWeightEnum: LxFontWeightEnum.titleLarge,
                ),
                SizedBox(height: 36.w),
                InkWell(
                  onTap: () {
                    // 选择国家码
                    Get.toNamed(AppRoutes.AREA_CODE)?.then((value) {
                      if (value != null && value is AreaCodeModel) {
                        areaCodeModel.value = value;
                      }
                    });
                  },
                  child: SizedBox(
                    height: 40.w,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Row(
                          children: [
                            Image.asset(
                              R.selectCountryIcon,
                              fit: BoxFit.fitWidth,
                              width: 24,
                            ),
                            const SizedBox(width: 24),
                            Obx(
                              () => LxText(
                                areaCodeModel.value == null
                                    ? local.please_choose
                                    : areaCodeModel.value?.name,
                                color: areaCodeModel.value == null
                                    ? Colors.white.withOpacity(0.6)
                                    : Colors.white,
                                fontWeightEnum: LxFontWeightEnum.bodyLarge,
                              ),
                            ),
                          ],
                        ),
                        Image.asset(
                          R.arrowRightIcon,
                          fit: BoxFit.fitWidth,
                          width: 16,
                        ),
                      ],
                    ),
                  ),
                ),
                SizedBox(height: CONTENT_MARGIN),
                const Divider(height: 1, color: Color(0xFF3F3F3F)),
              ],
            ),
            Align(
              alignment: Alignment.bottomCenter,
              child: SizedBox(
                height: 125.w,
                child: Column(
                  children: [
                    Obx(
                      () => LxButton(
                        title: local.confirm,
                        buttonAlignmentEnum: ButtonAlignmentEnum.block,
                        buttonSizeEnum: ButtonSizeEnum.large,
                        onTap: () {
                          if (areaCodeModel.value == null) return;

                          widget.onSelected(areaCodeModel.value!, (success) {
                            if (success) {
                              Navigator.pop(context);
                            }
                          });
                        },
                        buttonStausEnum: areaCodeModel.value != null
                            ? ButtonStausEnum.normal
                            : ButtonStausEnum.disable,
                      ),
                    ),
                    SizedBox(height: 16.w),
                    LxText(
                      local.select_country_desc,
                      color: Colors.white.withOpacity(0.7),
                      fontWeightEnum: LxFontWeightEnum.bodyLarge,
                    ),
                  ],
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}
